package edu.nanyang.hibernate.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.nanyang.domain.Goods;
import edu.nanyang.domain.Photo;
import edu.nanyang.domain.School;
import edu.nanyang.domain.User;
import edu.nanyang.hibernate.IGoodsDao;
import edu.nanyang.hibernate.IPhotoDao;
import edu.nanyang.hibernate.IReplysDao;
import edu.nanyang.hibernate.ISchoolDao;
import edu.nanyang.hibernate.IUserDao;
import edu.nanyang.util.CommonData;
import edu.nanyang.util.GoodsHelper;
import edu.nanyang.util.QueryResult;

public class IGoodsServiceImpl implements IGoodsService {
	private IGoodsDao goodsDao;
	private ISchoolDao schoolDao;
	private IPhotoDao photoDao;
	private IReplysDao replysDao;
	private IUserDao userDao;
	@Override
	public List<GoodsHelper> getGoodsByUser(User user) {
		List<GoodsHelper> helpers=new ArrayList<GoodsHelper>();
		List<Goods> goods=this.goodsDao.getGoodsByUserId(user.getId());
		for(Goods g:goods){
			GoodsHelper helper=new GoodsHelper();
			if(g!=null){
				helper.setId(g.getId());
				helper.setReplayNum(this.replysDao.getGoodsReplysNum(g.getId()));
				helper.setTitle(g.getTitle());
				helper.setDescription(g.getDescription());
				helper.setPrice(g.getPrice());
				helper.setSupplyordemand(g.getSupplyordemand());
				helper.setPublishTime(g.getPublishTime());
				helper.setClassify(g.getClassify());
				helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
				helper.setSoldOut(g.getSoldOut());
				
				helper.setUpdateTime(g.getUpdateTime());
				User u=new User();
				u.setId(g.getUser().getId());
				u.setUsername(g.getUser().getUsername());
				helper.setUser(u);
				
				if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
					helper.setCanBeJingpaiNow(false);
				}else{
					if(g.getIsJingpaiOrNot()==1){
						if(g.getSoldOut()==0){
							helper.setCanBeJingpaiNow(true);
						}else{
							helper.setCanBeJingpaiNow(false);
						}
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}
				if(g.getPhoto()!=null){
					helper.setPhoto1(g.getPhoto().getPathBefore());
					helper.setPhoto2(g.getPhoto().getPathAfter());
				}
				if(g.getUser()!=null){
					helper.setUser(g.getUser());
				}
			}
			
			helpers.add(helper);
		}
		return helpers;
	}
	public GoodsHelper acquireGoodsById(int id){
		Goods g=this.goodsDao.acquireGoodsById(id);
		GoodsHelper helper=new GoodsHelper();
		if(g!=null){
			helper.setId(g.getId());
			helper.setTitle(g.getTitle());
			helper.setDescription(g.getDescription());
			helper.setPrice(g.getPrice());
			helper.setSupplyordemand(g.getSupplyordemand());
			helper.setPublishTime(g.getPublishTime());
			helper.setClassify(g.getClassify());
			helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
			helper.setSoldOut(g.getSoldOut());
			if(g.getUserIdJingpai()!=0){
				helper.setUserIdJingpai(g.getUserIdJingpai());
				helper.setUserNameJingpai(this.userDao.getUserById(g.getUserIdJingpai()).getUsername());
			}
			User user=new User();
			user.setId(g.getUser().getId());
			user.setUsername(g.getUser().getUsername());
			helper.setUser(user);
			
			
			if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
				helper.setCanBeJingpaiNow(false);
			}else{
				if(g.getIsJingpaiOrNot()==1){
					if(g.getSoldOut()==0){
						helper.setCanBeJingpaiNow(true);
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}else{
					helper.setCanBeJingpaiNow(false);
				}
			}
			if(g.getPhoto()!=null){
				helper.setPhoto1(g.getPhoto().getPathBefore());
				helper.setPhoto2(g.getPhoto().getPathAfter());
			}
			if(g.getUser()!=null){
				helper.setUser(g.getUser());
			}
		}
		return helper;
	}
	@Override
	public boolean addGoods(Goods g, int schoolId,Photo photo,User user) {
		School s=this.schoolDao.acquireSchoolById(schoolId);
		if(s==null){
			return false;
		}
		int photoId=0;
		g.setSchool(s);
		g.setCity(s.getCity());
		g.setProvince(s.getCity().getProvince());
		g.setPublishTime(new Date());
		g.setUpdateTime(new Date());
		g.setUserIdJingpai(0);
		g.setShenHe(CommonData.GOODS_SHENHE_NO);
		g.setSoldOut(0);
		g.setUser(user);
		if(photo.getPathAfter()!=null &&  !"".equals(photo.getPathAfter().trim()) && photo.getPathBefore()!=null && !"".equals(photo.getPathBefore().trim())){
			photoId=this.photoDao.addPhoto(photo);
			g.setPhoto(this.photoDao.getPhotoById(photoId));
		}
		boolean result=this.goodsDao.addGoods(g);
		return result;	
	}
	@Override
	public long acquireTotalGoodsNumByCurrCityID(int cityID) {
		return this.goodsDao.acquireTotalGoodsNumByCityID(cityID);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsHelper> acquireGoodsByCurrCity(int cityID, int page, int pageSize) {
		List<Goods> list1 = this.goodsDao.acquireGoodsByCurrCityID(cityID, page, pageSize);
		List<GoodsHelper> list=new ArrayList<GoodsHelper>();
		for(Goods g:list1){
			GoodsHelper helper=new GoodsHelper();
			helper.setId(g.getId());
			helper.setTitle(g.getTitle());
			helper.setDescription(g.getDescription());
			helper.setPrice(g.getPrice());
			helper.setSupplyordemand(g.getSupplyordemand());
			helper.setPublishTime(g.getPublishTime());
			helper.setClassify(g.getClassify());
			helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
			helper.setSoldOut(g.getSoldOut());
			helper.setUpdateTime(g.getUpdateTime());
			User u=new User();
			u.setId(g.getUser().getId());
			u.setUsername(g.getUser().getUsername());
			helper.setUser(u);
			
			if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
				helper.setCanBeJingpaiNow(false);
			}else{
				if(g.getIsJingpaiOrNot()==1){
					if(g.getSoldOut()==0){
						helper.setCanBeJingpaiNow(true);
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}else{
					helper.setCanBeJingpaiNow(false);
				}
			}
			if(g.getPhoto()!=null){
				helper.setPhoto1(g.getPhoto().getPathBefore());
				helper.setPhoto2(g.getPhoto().getPathAfter());
			}
			
			list.add(helper);
		}
		return list;
	}
	public IGoodsDao getGoodsDao() {
		return goodsDao;
	}
	public void setGoodsDao(IGoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}
	public ISchoolDao getSchoolDao() {
		return schoolDao;
	}
	public void setSchoolDao(ISchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}
	@SuppressWarnings("unchecked")
	@Override
	public QueryResult<GoodsHelper> acquireGoodsByDetail(String buyOrSale, int schoolId,
			String goodsType, double priceFrom, double priceTo,
			boolean lookOnlineOnly, String sortType, String queryStringdetail,
			int queryDays, int page, int pageSize,int cityId) {
		QueryResult<GoodsHelper> goodsHelper=new QueryResult<GoodsHelper>();
		List<GoodsHelper> list=new ArrayList<GoodsHelper>();
		QueryResult<Goods> goods= this.goodsDao.acquireGoodsByDetail(buyOrSale, 
												  schoolId, 
												  goodsType, 
												  priceFrom, 
												  priceTo, 
												  lookOnlineOnly, 
												  sortType, 
												  queryStringdetail, 
												  queryDays, 
												  page, 
												  pageSize,
												  cityId);
		goodsHelper.setPage(goods.getPage());
		goodsHelper.setPageSize(goods.getPageSize());
		goodsHelper.setTotal(goods.getTotal());
		for(Goods g:goods.getObjects()){
			GoodsHelper helper=new GoodsHelper();
			helper.setId(g.getId());
			helper.setTitle(g.getTitle());
			helper.setDescription(g.getDescription());
			helper.setPrice(g.getPrice());
			helper.setSupplyordemand(g.getSupplyordemand());
			helper.setPublishTime(g.getPublishTime());
			helper.setClassify(g.getClassify());
			helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
			helper.setSoldOut(g.getSoldOut());
			helper.setUpdateTime(g.getUpdateTime());
			User u=new User();
			u.setId(g.getUser().getId());
			u.setUsername(g.getUser().getUsername());
			helper.setUser(u);
			
			if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
				helper.setCanBeJingpaiNow(false);
			}else{
				if(g.getIsJingpaiOrNot()==1){
					if(g.getSoldOut()==0){
						helper.setCanBeJingpaiNow(true);
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}else{
					helper.setCanBeJingpaiNow(false);
				}
			}
			if(g.getPhoto()!=null){
				helper.setPhoto1(g.getPhoto().getPathBefore());
				helper.setPhoto2(g.getPhoto().getPathAfter());
			}
			
			list.add(helper);
		}
		goodsHelper.setObjects(list);
		return goodsHelper;
	}
	public IPhotoDao getPhotoDao() {
		return photoDao;
	}
	public void setPhotoDao(IPhotoDao photoDao) {
		this.photoDao = photoDao;
	}
	public IReplysDao getReplysDao() {
		return replysDao;
	}
	public void setReplysDao(IReplysDao replysDao) {
		this.replysDao = replysDao;
	}
	@Override
	public boolean saleGoods(int goodsId, int userId) {
		Goods goods=this.goodsDao.acquireGoodsById(goodsId);
		goods.setUserIdJingpai(userId);
		goods.setJingPaiType("用户指定卖出");
		goods.setSoldOut(1);
		goods.setUpdateTime(new Date());
		//这里不用
		return this.goodsDao.updateGoods(goods);
	}
	@Override
	public List<GoodsHelper> getGoodsUnverified() {
		List<Goods> list1 = this.goodsDao.getGoodsUnvirified();
		List<GoodsHelper> list=new ArrayList<GoodsHelper>();
		for(Goods g:list1){
			GoodsHelper helper=new GoodsHelper();
			helper.setId(g.getId());
			helper.setTitle(g.getTitle());
			helper.setDescription(g.getDescription());
			helper.setPrice(g.getPrice());
			helper.setSupplyordemand(g.getSupplyordemand());
			helper.setPublishTime(g.getPublishTime());
			helper.setClassify(g.getClassify());
			helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
			helper.setSoldOut(g.getSoldOut());
			
			User u=new User();
			u.setId(g.getUser().getId());
			u.setUsername(g.getUser().getUsername());
			helper.setUser(u);
			
			if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
				helper.setCanBeJingpaiNow(false);
			}else{
				if(g.getIsJingpaiOrNot()==1){
					if(g.getSoldOut()==0){
						helper.setCanBeJingpaiNow(true);
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}else{
					helper.setCanBeJingpaiNow(false);
				}
			}
			if(g.getPhoto()!=null){
				helper.setPhoto1(g.getPhoto().getPathBefore());
				helper.setPhoto2(g.getPhoto().getPathAfter());
			}
			
			list.add(helper);
		}
		return list;
	}
	@Override
	public boolean shenHeGoods(int goodsId, int shenHe) {
		try{
			Goods g=this.goodsDao.acquireGoodsById(goodsId);
			g.setShenHe(shenHe);
			this.goodsDao.updateGoods(g);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean refreshGoods(int goodsId) {
		try{
			Goods g=this.goodsDao.acquireGoodsById(goodsId);
			g.setUpdateTime(new Date());
			this.goodsDao.updateGoods(g);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public List<GoodsHelper> getGoodsByJingpaiUserid(int userId) {
		List<Goods> list1 = this.goodsDao.getGoodsByJingpaiUserid(userId);
		List<GoodsHelper> list=new ArrayList<GoodsHelper>();
		for(Goods g:list1){
			GoodsHelper helper=new GoodsHelper();
			helper.setId(g.getId());
			helper.setReplayNum(this.replysDao.getGoodsReplysNum(g.getId()));
			helper.setTitle(g.getTitle());
			helper.setDescription(g.getDescription());
			helper.setPrice(g.getPrice());
			helper.setSupplyordemand(g.getSupplyordemand());
			helper.setPublishTime(g.getPublishTime());
			helper.setClassify(g.getClassify());
			helper.setIsJingpaiOrNot(g.getIsJingpaiOrNot());
			helper.setSoldOut(g.getSoldOut());
			
			User u=new User();
			u.setId(g.getUser().getId());
			u.setUsername(g.getUser().getUsername());
			helper.setUser(u);
			
			if((new Date().getTime()-7L*24L*60L*60L*1000L)>(g.getPublishTime().getTime())){
				helper.setCanBeJingpaiNow(false);
			}else{
				if(g.getIsJingpaiOrNot()==1){
					if(g.getSoldOut()==0){
						helper.setCanBeJingpaiNow(true);
					}else{
						helper.setCanBeJingpaiNow(false);
					}
				}else{
					helper.setCanBeJingpaiNow(false);
				}
			}
			if(g.getPhoto()!=null){
				helper.setPhoto1(g.getPhoto().getPathBefore());
				helper.setPhoto2(g.getPhoto().getPathAfter());
			}
			
			list.add(helper);
		}
		return list;
	}
	public IUserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	

	
	
	
}
